package cn.ekuma.epos.qkorder.link;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import cn.ekuma.epos.order.JOrderDetail;
import cn.ekuma.epos.qkorder.JLinkContainerPanel;
import cn.ekuma.epos.qkorder.event.OrderChangedEvent;
import cn.ekuma.epos.qkorder.event.OrderChangedListener;
import com.openbravo.bean.Location;
import com.openbravo.bean.erp.Order;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.pos.base.AppLocal;

import java.awt.BorderLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Dimension;

@SuppressWarnings("serial")
public class JOrderDetailPanel extends JPanel implements OrderChangedListener {

	JLinkContainerPanel container;
	
	JOrderDetail orderDetail;
	
	/** Creates new form JOrderDetialPanel */
	public JOrderDetailPanel(JLinkContainerPanel container) {
		initComponents();
		this.container = container;
		List<Location> locations;
		try {
			locations = container.getDlSales().list(Location.class);
		} catch (BasicException ex) {
			new MessageInf(ex).show(this);
			locations = new ArrayList();
		}
		orderDetail=new JOrderDetail();
		orderDetail.setLocations(locations);
		jScrollPane3.setViewportView(orderDetail);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		jPanel5 = new javax.swing.JPanel();
		jPanel5.setRequestFocusEnabled(false);
		add(jPanel5, BorderLayout.CENTER);
		jScrollPane2 = new javax.swing.JScrollPane();
		jMemo = new javax.swing.JTextArea();
		jScrollPane3 = new javax.swing.JScrollPane();
		
				jPanel5.setLayout(new java.awt.BorderLayout());
				
						jScrollPane2.setBorder(javax.swing.BorderFactory
								.createTitledBorder(AppLocal.getIntString("label.order.memo"))); // NOI18N
						jScrollPane2.setPreferredSize(new java.awt.Dimension(150, 133));
						
								jMemo.setColumns(20);
								jMemo.setRows(5);
								jMemo.setFocusable(false);
								jMemo.setRequestFocusEnabled(false);
								jScrollPane2.setViewportView(jMemo);
								
										jPanel5.add(jScrollPane2, java.awt.BorderLayout.EAST);
																																																		
																																																				jPanel5.add(jScrollPane3, java.awt.BorderLayout.CENTER);
																																																				jPanel1 = new javax.swing.JPanel();
																																																				jPanel1.setPreferredSize(new Dimension(260, 10));
																																																				jPanel1.setRequestFocusEnabled(false);
																																																				add(jPanel1, BorderLayout.EAST);
																																																				jPanel3 = new javax.swing.JPanel();
																																																				m_jLblTotalEuros2 = new javax.swing.JLabel();
																																																				m_jSubtotalEuros = new javax.swing.JTextField();
																																																				m_jLblTotalEuros1 = new javax.swing.JLabel();
																																																				m_jTotalEuros = new javax.swing.JLabel();
																																																				jScrollPane1 = new javax.swing.JScrollPane();
																																																				jTable1 = new javax.swing.JTable();
																																																				
																																																						jPanel1.setLayout(new java.awt.BorderLayout());
																																																						
																																																								jPanel3.setMinimumSize(new java.awt.Dimension(10, 50));
																																																								
																																																										m_jLblTotalEuros2.setText(AppLocal.getIntString("label.articlescount")); // NOI18N
																																																										
																																																												m_jSubtotalEuros.setEditable(false);
																																																												m_jSubtotalEuros.setFocusable(false);
																																																												m_jSubtotalEuros.setRequestFocusEnabled(false);
																																																												
																																																														m_jLblTotalEuros1.setText(AppLocal.getIntString("label.totalcash")); // NOI18N
																																																														
																																																																m_jTotalEuros.setBackground(java.awt.Color.white);
																																																																m_jTotalEuros.setFont(new java.awt.Font("Dialog", 1, 14));
																																																																m_jTotalEuros
																																																																		.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
																																																																m_jTotalEuros.setBorder(javax.swing.BorderFactory.createCompoundBorder(
																																																																		javax.swing.BorderFactory
																																																																				.createLineBorder(javax.swing.UIManager.getDefaults()
																																																																						.getColor("Button.darkShadow")),
																																																																		javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4)));
																																																																m_jTotalEuros.setFocusable(false);
																																																																m_jTotalEuros.setOpaque(true);
																																																																m_jTotalEuros.setPreferredSize(new java.awt.Dimension(150, 20));
																																																																m_jTotalEuros.setRequestFocusEnabled(false);
																																																																
																																																																		javax.swing.GroupLayout gl_jPanel3 = new javax.swing.GroupLayout(
																																																																				jPanel3);
																																																																		gl_jPanel3.setHorizontalGroup(
																																																																			gl_jPanel3.createParallelGroup(Alignment.LEADING)
																																																																				.addGroup(gl_jPanel3.createSequentialGroup()
																																																																					.addContainerGap()
																																																																					.addComponent(m_jLblTotalEuros2, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)
																																																																					.addPreferredGap(ComponentPlacement.RELATED)
																																																																					.addComponent(m_jSubtotalEuros, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
																																																																					.addPreferredGap(ComponentPlacement.RELATED)
																																																																					.addComponent(m_jLblTotalEuros1, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
																																																																					.addPreferredGap(ComponentPlacement.RELATED)
																																																																					.addComponent(m_jTotalEuros, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
																																																																					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
																																																																		);
																																																																		gl_jPanel3.setVerticalGroup(
																																																																			gl_jPanel3.createParallelGroup(Alignment.LEADING)
																																																																				.addGroup(gl_jPanel3.createSequentialGroup()
																																																																					.addContainerGap(18, Short.MAX_VALUE)
																																																																					.addGroup(gl_jPanel3.createParallelGroup(Alignment.LEADING)
																																																																						.addGroup(Alignment.TRAILING, gl_jPanel3.createSequentialGroup()
																																																																							.addComponent(m_jLblTotalEuros1)
																																																																							.addGap(8))
																																																																						.addGroup(Alignment.TRAILING, gl_jPanel3.createSequentialGroup()
																																																																							.addComponent(m_jTotalEuros, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
																																																																							.addGap(6))
																																																																						.addGroup(Alignment.TRAILING, gl_jPanel3.createSequentialGroup()
																																																																							.addComponent(m_jSubtotalEuros, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
																																																																							.addGap(5))
																																																																						.addGroup(Alignment.TRAILING, gl_jPanel3.createSequentialGroup()
																																																																							.addComponent(m_jLblTotalEuros2)
																																																																							.addGap(8))))
																																																																		);
																																																																		jPanel3.setLayout(gl_jPanel3);
																																																																		
																																																																				jPanel1.add(jPanel3, java.awt.BorderLayout.NORTH);
																																																																				
																																																																						jScrollPane1.setFocusable(false);
																																																																						jScrollPane1.setRequestFocusEnabled(false);
																																																																						
																																																																								jTable1.setModel(new javax.swing.table.DefaultTableModel(
																																																																										new Object[][] { { null, null, null, null },
																																																																												{ null, null, null, null }, { null, null, null, null },
																																																																												{ null, null, null, null } }, new String[] { "Title 1",
																																																																												"Title 2", "Title 3", "Title 4" }));
																																																																								jTable1.setEnabled(false);
																																																																								jTable1.setFocusable(false);
																																																																								jTable1.setRequestFocusEnabled(false);
																																																																								jScrollPane1.setViewportView(jTable1);
																																																																								jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
									
	}// </editor-fold>
	private javax.swing.JTextArea jMemo;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JPanel jPanel5;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JScrollPane jScrollPane3;
	private javax.swing.JTable jTable1;
	private javax.swing.JLabel m_jLblTotalEuros1;
	private javax.swing.JLabel m_jLblTotalEuros2;
	private javax.swing.JTextField m_jSubtotalEuros;
	private javax.swing.JLabel m_jTotalEuros;

	// End of variables declaration

	@Override
	public void orderChanged(OrderChangedEvent event) {
		printOrderState();
	}

	public void printOrderState() {
		Order order = container.getOrderManager().getOrder();
		orderDetail.printOrderState(order);
		jMemo.setEditable(true);
		jMemo.setText(order.getMemo());
		jTable1.setModel(container.getOrderManager());
	}

	public void printPartialTotals() {
		m_jSubtotalEuros.setText(container.getOrderManager()
				.printArticlesCount());
		m_jTotalEuros.setText(container.getOrderManager().printTotal());
	}

}
